//
//  AuthClient.swift
//  guard
//
//  Created by linus on 2021/11/20.
//

import UIKit

class AuthClient: NSObject {
    public static func loginByAccount(account: String, password: String, callback: @escaping AuthCallback<UserInfo?>) {
        Authing.getPublicConfig { (o_config) in
            guard let config = o_config, let identifier = config.identifier else {
                print("AuthClient: no config")
                return
            }
            
            let url = "https://" + identifier + ".authing.cn/api/v2/login/account"
            let data: NSDictionary = ["account": account, "password": Util.encryptPassword(password)]
            Guardian.post(url: url, body: data) { (response) in
                guard response.code == 200 else {
                    callback(response.code!, response.message!, nil)
                    return
                }
                guard let data = response.data else {
                    callback(500, "Cannot parse data into UserInfo", nil)
                    return
                }
                callback(response.code!, response.message!, UserInfo.createUserInfo(data: data))
            }
        }
    }
    
    public static func registerByEmail(email: String, password: String, callback: @escaping AuthCallback<UserInfo?>) {
        Authing.getPublicConfig { (o_config) in
            guard let config = o_config, let identifier = config.identifier else {
                print("AuthClient: no config")
                return
            }
            
            let url = "https://" + identifier + ".authing.cn/api/v2/register/email"
            let data: NSDictionary = ["email": email, "password": Util.encryptPassword(password)]
            Guardian.post(url: url, body: data) { (response) in
                guard response.code == 200 else {
                    callback(response.code!, response.message!, nil)
                    return
                }
                guard let data = response.data else {
                    callback(500, "Cannot parse data into UserInfo", nil)
                    return
                }
                callback(response.code!, response.message!, UserInfo.createUserInfo(data: data))
            }
        }
    }
    
    public static func registerByPhoneCode(phone: String, password: String, code: String, callback: @escaping AuthCallback<UserInfo?>) {
        Authing.getPublicConfig { (o_config) in
            guard let config = o_config, let identifier = config.identifier else {
                print("AuthClient: no config")
                return
            }
            
            let url = "https://" + identifier + ".authing.cn/api/v2/register/phone-code"
            let data: NSDictionary = ["phone": phone, "code": code, "password": Util.encryptPassword(password)]
            Guardian.post(url: url, body: data) { (response) in
                guard response.code == 200 else {
                    callback(response.code!, response.message!, nil)
                    return
                }
                guard let data = response.data else {
                    callback(500, "Cannot parse data into UserInfo", nil)
                    return
                }
                callback(response.code!, response.message!, UserInfo.createUserInfo(data: data))
            }
        }
    }
    
    public static func resetPasswordByEmail(email: String, callback: @escaping AuthCallback<NSDictionary?>) {
        sendEmail(email: email, scene: "RESET_PASSWORD", callback: callback)
    }
    
    public static func sendEmail(email: String, scene: String, callback: @escaping AuthCallback<NSDictionary?>) {
        Authing.getPublicConfig { (o_config) in
            guard let config = o_config, let identifier = config.identifier else {
                print("AuthClient: no config")
                return
            }
            
            let url = "https://" + identifier + ".authing.cn/api/v2/email/send"
            let data: NSDictionary = ["email": email, "scene": scene]
            Guardian.post(url: url, body: data) { (response) in
                callback(response.code!, response.message!, response.data)
            }
        }
    }
    
    public static func resetPasswordByEmailCode(email: String, code: String, newPassword: String, callback: @escaping AuthCallback<NSDictionary?>) {
        Authing.getPublicConfig { (o_config) in
            guard let config = o_config, let identifier = config.identifier else {
                print("AuthClient: no config")
                return
            }
            
            let url = "https://" + identifier + ".authing.cn/api/v2/password/reset/email"
            let data: NSDictionary = ["email": email, "code": code, "newPassword": Util.encryptPassword(newPassword)]
            Guardian.post(url: url, body: data) { (response) in
                callback(response.code!, response.message!, response.data)
            }
        }
    }
}
